/*
 * Copyright (c) 2008 by 1eEurope (Switzerland) AG, Thalwil
 *
 * Customer:	1eEurope (Switzerland) AG
 * Project:		BKS
 * Created:		30.09.2008, WBL
 *
*/

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Xml.Xsl;
using System.Xml;
using System.IO;

namespace By1eEurope.BKS.Tools.Utils
{
    /// <summary>
    /// Utilities if you work with XML
    /// </summary>
    public class XmlUtil
    {
        /// <summary>
        /// Gets an xml-File as XmlDocument from the SiteCollection
        /// </summary>
        /// <param name="web">current web</param>
        /// <param name="configUrl">URL to the xml-file</param>
        /// <returns>XmlDocument representing the xml-file, null in case of any error</returns>
        public static XmlDocument GetXmlConfigDocument(SPWeb web, string configUrl)
        {
            try
            {
                string s = string.Empty;

                if (configUrl.StartsWith("/"))
                    s = web.GetFileAsString(web.Site.Url + configUrl);
                else
                    s = web.GetFileAsString(web.Site.Url + "/" + configUrl);

                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(s);
                return xDoc;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Gets an xslt-File as XslCompiledTransform from the SiteCollection
        /// </summary>
        /// <param name="web">current web</param>
        /// <param name="configUrl">URL to the xslt-file</param>
        /// <returns>XslCompiledTransform representing the xslt-file, null in case of any error</returns>
        public static XslCompiledTransform GetXsltDocument(SPWeb web, string configUrl)
        {
            try
            {
                string s = string.Empty;

                if (configUrl.StartsWith("/"))
                    s = web.GetFileAsString(web.Site.Url + configUrl);
                else
                    s = web.GetFileAsString(web.Site.Url + "/" + configUrl);

                XslCompiledTransform xslt = new XslCompiledTransform();
                StringReader sr = new StringReader(s);
                XmlReader xr = XmlReader.Create(sr);
                xslt.Load(xr);
                return xslt;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Transforms an xml valid string
        /// </summary>
        /// <param name="xslt">xslt tranformation</param>
        /// <param name="xml">xml valid string</param>
        /// <param name="xsltArgumentList">arguments of the transformation</param>
        /// <returns>transformed string. Format depends on the transformation.</returns>
        public static StringBuilder TransformXslt(XslCompiledTransform xslt, string xml, XsltArgumentList xsltArgumentList)
        {
            StringBuilder stringBuilder = new StringBuilder();
            
            if (xslt == null)
            {
                stringBuilder.Append("The xslt file may not be null.");
                return stringBuilder;
            }

            // Prepare input XML
            StringReader stringReader = new StringReader(xml);
            XmlReader xmlReader = XmlReader.Create(stringReader);

            // Prepare output Writer
            XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, xslt.OutputSettings);

            xslt.Transform(xmlReader, xsltArgumentList, xmlWriter);

            return stringBuilder;
        }
    }
}
